package com.zx.system.block.controller.system;

import com.zx.core.wrapper.ListWrapper;
import com.zx.core.wrapper.ObjectWrapper;
import com.zx.core.wrapper.PageWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.log.annotation.OperateLog;
import com.zx.system.block.param.PostParam;
import com.zx.system.block.service.ISysPostService;
import com.zx.system.block.vo.SysPostVO;
import com.zx.validation.annotation.ParameterValidation;
import com.zx.validation.annotation.ParameterValidations;
import com.zx.validation.enums.ValidateRulesEnum;
import com.zx.validation.groups.Create;
import com.zx.validation.groups.Query;
import com.zx.validation.groups.StatusUpdate;
import com.zx.validation.groups.Update;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * <p>
 * description: 系统岗位 <br>
 * create: 2024-10-29 13:50
 * </p>
 *
 * @author zhou  xun
 */
@RestController
@RequestMapping("/post")
@RequiredArgsConstructor
public class SysPostController {
    private final ISysPostService iSysPostService;

    /**
     * 获取岗位列表
     *
     * @param postParam 查询参数
     * @return {@link ListWrapper<SysPostVO>}
     * @author zhou  xun
     * @since 2024-10-29
     */
    @PreAuthorize("hasAuthority('system:post:list')")
    @OperateLog(value = "获取岗位列表")
    @PostMapping("/list")
    public ListWrapper<SysPostVO> list(PostParam postParam) {
        return iSysPostService.selectPostVOList(postParam);
    }

    /**
     * 分页获取岗位列表
     *
     * @param postParam 查询参数
     * @return {@link PageWrapper<SysPostVO>}
     * @author zhou  xun
     * @since 2025-11-15
     */
    @PreAuthorize("hasAuthority('system:post:list')")
    @OperateLog(value = "分页获取岗位列表")
    @PostMapping("/listByPage")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "岗位参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Query.class})
    )
    public PageWrapper<SysPostVO> listByPage(@RequestBody PostParam postParam) {
        return iSysPostService.selectPostVOListByPage(postParam);
    }

    /**
     * 新增岗位
     *
     * @param postParam 岗位信息
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-10-29
     */
    @PreAuthorize("hasAuthority('system:post:add')")
    @OperateLog(value = "新增岗位")
    @PostMapping("/addPost")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "岗位参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Create.class})
    )
    public ResponseWrapper addPost(@RequestBody PostParam postParam) {
        return iSysPostService.addPost(postParam);
    }

    /**
     * 编辑岗位
     *
     * @param postParam 岗位信息
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-10-29
     */
    @PreAuthorize("hasAuthority('system:post:edit')")
    @OperateLog(value = "编辑岗位")
    @PutMapping("/editPost")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "岗位参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Update.class})
    )
    public ResponseWrapper editPost(@RequestBody PostParam postParam) {
        return iSysPostService.editPost(postParam);
    }

    /**
     * 修改岗位状态
     *
     * @param postParam 岗位参数
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-10-29
     */
    @PreAuthorize("hasAuthority('system:post:edit')")
    @OperateLog(value = "修改岗位状态")
    @PutMapping("/editPostStatus")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "岗位参数", rules = {ValidateRulesEnum.OBJECT}, groups = {StatusUpdate.class})
    )
    public ResponseWrapper editPostStatus(@RequestBody PostParam postParam) {
        return iSysPostService.editPostStatus(postParam.getId(), postParam.getStatus());
    }

    /**
     * 删除岗位
     *
     * @param postId 岗位Id
     * @return {@link ResponseWrapper}
     * @author zhou  xun
     * @since 2024-10-29
     */
    @PreAuthorize("hasAuthority('system:post:remove')")
    @OperateLog(value = "删除岗位")
    @DeleteMapping("/deletePost/{postId}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "岗位Id", rules = {ValidateRulesEnum.NOT_NULL}, groups = {Update.class})
    })
    public ResponseWrapper deletePost(@PathVariable("postId") String postId) {
        return iSysPostService.deletePost(postId);
    }

    /**
     * 获取用户岗位列表
     *
     * @param userId 用户id
     * @return {@link ObjectWrapper}
     * @author zhou  xun
     * @since 2025-04-14
     */
    @PreAuthorize("hasAuthority('system:post:getUserPost')")
    @OperateLog(value = "获取用户岗位列表")
    @GetMapping("/getUserPostIdList/{userId}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "用户Id", rules = {ValidateRulesEnum.NOT_NULL})
    })
    public ObjectWrapper<Set<String>> getUserPostIdList(@PathVariable("userId") String userId) {
        return iSysPostService.selectPostIdsByUserId(userId);
    }
}